/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Lobby.java
 *
 * Created on 18-mrt-2011, 1:12:27
 */
package view;

import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import main.ApplicatieMain;
import model.User;
import serverComm.CommConnection;

/**
 *
 * @author Nick
 */
public class Lobby extends javax.swing.JPanel {

    List<User> model;
//    private ServerComm server;
    private CommConnection server;
    private MultiSpelPagina multiSpel;
    private ApplicatieMain applicatieMain;

    /** Creates new form Lobby */
    public Lobby() {
        applicatieMain = ApplicatieMain.getInstance();
        applicatieMain.setMultiSpel(null);
        applicatieMain.setLobby(this);
        
        initComponents();


    }

    public void startLobby(){
        applicatieMain.setNotifyLobbyUpdate(true);
        if (applicatieMain.getCommServer() == null) {
            server = new CommConnection();
            Thread serverThread = new Thread(server);
            startConnection(serverThread);
            applicatieMain.setCommServer(server);
            System.out.println("SET COMMSERVER");
        } else {
            System.out.println("TEST");
            server = applicatieMain.getCommServer();
        }
    }

    public void updateWordListLabel(String woordenlijst){
        lblWoordenlijst.setText(woordenlijst);
    }
    private void startConnection(Thread serverThread) {
        serverThread.start();
    }

    public void requestGame(String opponent) {
        Object[] options = {"Ja", "Nee"};
        int keuze = JOptionPane.showOptionDialog(this, "Wilt u spelen tegen " + opponent, "Let op!",
                JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
                null, options, options[0]);
        switch (keuze) {
            default: {
                System.out.println("Unknown option in keuze OptionDialog");
            }
            case 0: {
                applicatieMain.getCommServer().getCommOut().acceptGame();
                break;
            }
            case 1: {
                applicatieMain.getCommServer().getCommOut().rejectGame();
                break;
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        buttonTerug = new javax.swing.JButton();
        btnKiesWoordenlijst = new javax.swing.JButton();
        lblWoordenlijst = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(1106, 502));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(1106, 502));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setPreferredSize(new java.awt.Dimension(858, 379));

        jPanel7.setBackground(new java.awt.Color(255, 255, 255));
        jPanel7.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(183, 234, 0), 15, true));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Lobby");

        jScrollPane1.setViewportView(jList1);

        jButton1.setText("Speel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jLabel2))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGap(163, 163, 163)
                        .addComponent(jButton1)))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(280, 280, 280)
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonTerug.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        buttonTerug.setForeground(new java.awt.Color(255, 0, 0));
        buttonTerug.setText("Terug");
        buttonTerug.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        buttonTerug.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        buttonTerug.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTerugActionPerformed(evt);
            }
        });

        btnKiesWoordenlijst.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnKiesWoordenlijst.setForeground(new java.awt.Color(0, 153, 0));
        btnKiesWoordenlijst.setText("Kies Woordenlijst");
        btnKiesWoordenlijst.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 204, 0)));
        btnKiesWoordenlijst.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnKiesWoordenlijst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKiesWoordenlijstActionPerformed(evt);
            }
        });

        lblWoordenlijst.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblWoordenlijst.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblWoordenlijst.setText("<UserCode>");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 636, Short.MAX_VALUE)
                .addGap(366, 366, 366))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(384, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblWoordenlijst, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                    .addComponent(btnKiesWoordenlijst, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonTerug, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(375, 375, 375))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonTerug, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                    .addComponent(btnKiesWoordenlijst, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblWoordenlijst)
                .addGap(35, 35, 35))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1106, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 502, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(applicatieMain.getSelectedWoordenlijst() == -1){
            JOptionPane.showMessageDialog(this, "Selecteer een woordenlijst voor het spelen.", "FOUT", 1);
        }
         else{
            if (jList1.getSelectedIndex() != -1) {
                User opponent = (User) model.get(jList1.getSelectedIndex());
                System.out.println("REQUESTING GAME: ID: " + opponent.getUserID() + ", NAME: " + opponent.getName());
                applicatieMain.setRequestedOpponent(opponent);
                server.getCommOut().requestGame(opponent);

            } else {
                JOptionPane.showMessageDialog(this, "Selecteer een tegenstander", "FOUT", 1);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void buttonTerugActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTerugActionPerformed
        if(server != null && applicatieMain.isServerRunning()){
            server.disconnectServer();
        } else {
            applicatieMain.setCommServer(null);
        }

        applicatieMain.showPage(new SpelerOptiesPagina());

    }//GEN-LAST:event_buttonTerugActionPerformed

    private void btnKiesWoordenlijstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKiesWoordenlijstActionPerformed
        JFrame frame = new view.KiesWordList(true);
        frame.setVisible(true);
    }//GEN-LAST:event_btnKiesWoordenlijstActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnKiesWoordenlijst;
    private javax.swing.JButton buttonTerug;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblWoordenlijst;
    // End of variables declaration//GEN-END:variables

    public void getLobby() {
        server.getCommOut().getLobby();
    }

    public void setLobby(List<User> lobbyUserList) {

        model = lobbyUserList;

        jList1.setListData(lobbyUserList.toArray());

    }
}
